package com.cskaoyan.mall.service.lmpl;

import com.cskaoyan.mall.dao.AdminMapper;
import com.cskaoyan.mall.model.po.Admin;
import com.cskaoyan.mall.model.bo.admin.AdminChangePwdBo;
import com.cskaoyan.mall.model.bo.admin.AdminLoginBo;
import com.cskaoyan.mall.service.AdminService;
import com.cskaoyan.mall.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName: AdminServiceImpl
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/6/28 18:53
 * @Version
 **/


public class AdminServiceImpl implements AdminService {

    //登录
    public Admin login(AdminLoginBo adminLoginBo) {
        //从数据库中取数据比对
        //获取SqlSession
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = mapper.selectOne(new Admin(adminLoginBo.getEmail(), adminLoginBo.getPwd(),null));
        sqlSession.commit();
        sqlSession.close();
        return admin;

    }

    //查询所有管理员
    @Override
    public List<AdminLoginBo> search() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminLoginBo> admins = mapper.selectByName(new Admin());
        sqlSession.commit();
        sqlSession.close();
        return admins;
    }

    /**
     * @Author: 庄左
     * @Description: 通过管理名字或者昵称来查找管理员
     * @DateTime: 2022/7/2 20:56
     */
    @Override
    public List<AdminLoginBo> selectByName(AdminLoginBo adminLoginBo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<AdminLoginBo> admin = mapper.selectByName(new Admin(null, adminLoginBo.getEmail(), null, adminLoginBo.getNickname()));
        sqlSession.commit();
        sqlSession.close();
        return admin;
    }

    /**
     * @Author: 庄左
     * @Description: 新增管理员
     * @DateTime: 2022/7/2 20:56
     */
    @Override
    public void addAdmins(AdminLoginBo adminLoginBo) {
        //从请求体中获得输入的管理员名，昵称，以及密码
        String email = adminLoginBo.getEmail();
        String nickname = adminLoginBo.getNickname();
        String pwd = adminLoginBo.getPwd();
        //传入对象的方式传入参数
        Admin admin = new Admin(null, email, nickname, pwd);

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        //
        mapper.addsAdmins(admin);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteAdmins(Integer id) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        mapper.deleteAdmins(id);
        sqlSession.commit();
        sqlSession.close();
    }

    //通过id查找
    @Override
    public Admin selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = mapper.selectById(id);
        sqlSession.commit();
        sqlSession.close();
        return admin;
    }

    /**
     * @Author: 庄左
     * @Description: 修改任意管理员的信息，需要权限控制
     * @DateTime: 2022/7/2 20:50
     */
    @Override
    public void updateAdmins(AdminLoginBo adminLoginBo) {
        //获取请求体传入的admin昵称,id,密码,以及用户名
        String nickname = adminLoginBo.getNickname();
        Integer id = adminLoginBo.getId();
        String pwd = adminLoginBo.getPwd();
        String email = adminLoginBo.getEmail();

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        //通过传入对象的方式传入这些值
        mapper.updateAdmins(new Admin(id, email, pwd, nickname));
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * @Author: 庄左
     * @Description: 修改当前admin自己的密码
     * @DateTime: 2022/7/2 20:49
     */
    @Override
    public void changePwd(AdminChangePwdBo adminChangePwdBo) {
        //请求体中 用户输入的用户名和新密码
        String adminToken = adminChangePwdBo.getAdminToken();
        String newPwd = adminChangePwdBo.getNewPwd();

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        //传入用户名以及新密码
        mapper.changePwd(new Admin(adminToken, newPwd));
        sqlSession.commit();
        sqlSession.close();
    }


}
